package org.example.mybatisdemo.dao;

import org.apache.ibatis.annotations.*;
import org.example.mybatisdemo.model.UserInfo;

import java.util.List;
@Mapper
public interface UserInfoMapper {
//    @Select("select id, username, password, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
    @Results(id="BaseMap",value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    @Select("select id, username, password, age, gender, phone, delete_flag , create_time, update_time from user_info")
    List<UserInfo> selectAll();
    @ResultMap("BaseMap")
    @Select("select id, username, password, age, gender, phone, delete_flag , create_time, update_time from user_info where id = #{id}")
    UserInfo selectById(Integer id);
//    @Select("select * from user_info where age = #{age} and gender = #{gender}")
//@Select("select * from user_info where age = #{param1} and gender = #{param2}")
    @Select("select * from user_info where age = #{age} and gender = #{gender}")
    List<UserInfo> selectByAgeAndGender(@Param("age") Integer age,@Param("gender") Integer gender);
    @Insert("insert into user_info (username,`password`,age,gender)values (#{username},#{password},#{age},#{gender})")
    Integer inserUser(UserInfo userInfo);
    @Options(useGeneratedKeys = true ,keyProperty = "id")
    @Insert("insert into user_info (username,`password`,age,gender)values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer inserUserByParam(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}")
    Integer delete(Integer id);

    @Update("update user_info set `password` = #{password} where id = #{id}")
    Integer update(Integer id,String password);

    @Select("select * from user_info where id = #{id}")
    UserInfo selectById1(Integer id);

    @Select("select * from user_info where id = ${id}")
    UserInfo selectById2(Integer id);

    //排序
    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserByOrder(String order);

//    @Select("select * from user_info where username like '%${name}%'")
    @Select("select * from user_info where username like CONCAT('%',#{name},'%')")
    List<UserInfo> selectUserByLike(String name);
}
